package com.inspur.icity.bus.restapi;

import com.inspur.icity.bus.api.user.model.BusSearchHistory;
import com.inspur.icity.bus.api.user.service.BusRouteService;
import com.inspur.icity.bus.api.user.service.BusSiteService;
import com.inspur.icity.bus.api.user.service.BusSearchHistoryService;
import com.inspur.icity.bus.base.core.model.BaseResult;
import com.inspur.icity.bus.base.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 路线站点Controller
 */
@Controller
@RequestMapping(value = "/routeSite")
public class BusRouteSiteController extends BaseController {

    @Autowired
    private BusRouteService busRouteService;

    @Autowired
    private BusSiteService busSiteService;

    @Autowired
    private BusSearchHistoryService busSearchHistoryService;

    /**
     * 查询路线、站点（搜索框调用）
     * @param keywords 搜索关键字
     * @param areacode 系统代码 01：乌海公交; 02:石家庄公交; 03:滨州公交; 90:济南城际公交（HBT）
     */
    @RequestMapping(value = "/queryBusRouterSite",method = RequestMethod.GET)
    @ResponseBody
    public Object queryBusRouterSite(@RequestParam(value = "keywords", required = true) String keywords,
                                     @RequestParam(value = "areacode", required = true) String areacode){
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("keyword",keywords);
        queryMap.put("areacode",areacode);
        busRouteService.queryBusRouter(queryMap,result); //查询线路信息
        busSiteService.queryBusSite(queryMap,result); //查询站点信息
        if(result.size() >10 ){
            return BaseResult.success(result.subList(0,10)); //默认获取前10条记录，暂定
        }
        return BaseResult.success(result);
    }

    /**
     * 获取用户常用线路、
     * @param mobile
     * @return站点列表
     */
    @RequestMapping(value = "/getUserRouteSite",method = RequestMethod.GET)
    @ResponseBody
    public Object getUserRouteSite(@RequestParam(value = "mobile",required = true)String mobile, String areacode){
        Map<String,Object> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("areacode",areacode);
        List<BusSearchHistory> list = busSearchHistoryService.find(map);
        Map<String,Object> resultMap = null;
        List<Map<String,Object>> resultList = new ArrayList<>();
        for(BusSearchHistory userSearch : list){
            resultMap = new HashMap<>();
            resultMap.put("type",userSearch.getType());
            resultMap.put("name",userSearch.getName());
            resultMap.put("direction",userSearch.getDirection());
            resultMap.put("areacode",userSearch.getAreacode());
            resultMap.put("updown",userSearch.getUpdown()==null?"":userSearch.getUpdown());
            resultList.add(resultMap);
        }
        return BaseResult.success(resultList);
    }


    /**
     * 获取线路详情
     * @param areacode 系统代号
     * @param routeName 线路名称
     * @param direction 线路方向
     * @return
     */
    @RequestMapping(value = "/getRouteDetails",method = RequestMethod.GET)
    @ResponseBody
    public Object getRouteDetails(@RequestParam(value = "areacode", required = true)String areacode,
                                  @RequestParam(value = "routeName", required = true)String routeName,
                                  @RequestParam(value = "updown", required = true)Integer updown,
                                  @RequestParam(value = "direction", required = true)String direction,
                                  @RequestParam(value = "flag", required = false)String flag,
                                  @RequestParam(value = "mobile", required = false)String mobile,
                                  @RequestParam(value = "longitude", required = true)Double longitude,
                                  @RequestParam(value = "latitude", required = true)Double latitude,
                                  @RequestParam(value = "isTnterval", required = true)Boolean isTnterval){
        if(!direction.endsWith("方向")){
            direction = direction+"方向";
        }
        Map<String,Object> map = busRouteService.getRouteDetails(areacode,routeName,direction,updown,flag,mobile,longitude,latitude,"yes",isTnterval);
        return BaseResult.success(map);
    }

    /**
     * 获取站点详情
     * @param name 站点名称
     * @param areacode 系统代码 01：乌海公交; 02:石家庄公交; 03:滨州公交; 90:济南城际公交（HBT）
     */
    @RequestMapping(value = "/getSiteDetails",method = RequestMethod.GET)
    @ResponseBody
    public Object getSiteDetails(@RequestParam(value = "name", required = true) String name,
                                 @RequestParam(value = "areacode", required = true) String areacode,
                                 @RequestParam(value = "flag", required = false) String flag,
                                 @RequestParam(value = "mobile", required = false) String mobile){
        Map<String,Object> map = busSiteService.getSiteDetails(name,areacode,flag,mobile);
        return BaseResult.success(map);
    }
}